<?php
/**
 * Allows to save and read objects data from different kind of source, 
 * e.g. reading/writing object from/to database.
 * 
 * @author Damian Sromek <damianpaq@gmail.com>
 * @version 0.1
 * @date 05-13-2009
 */
// TODO: comment
abstract class Plus_Model_Mapper_Abstract implements Plus_Model_Mapper_Interface
{
	/**
	 * Instances of all used mappers.
	 * Once created mapper is always returned. No need to create many instances of
	 * one mapper.
	 * 
	 * @var array(
	 * 	'Plus_Model_News_Mapper' => instance_news_mapper,
	 *  'Plus_Model_Country_Mapper' => instance_country_mapper,
	 *  ...
	 *  )
	 */
	private static $instancesArray = array();
	
	/** Logger */
	protected $_log;
	
	/**
	 * Constructor.
	 * Initialize logger.
	 * @return void
	 */
	protected function __construct()
	{
		$this->_log = Zend_Registry::get('log');
		
		$this->_log->info(get_class($this) . ' - start');
	}
	
	/**
	 * Returns always the same instance for each model.
	 * 
	 * N O T I C E !!!
	 * If subclass should also have only one instance then it should have 
	 * - protected or private constructor 
	 * 		AND
	 * - following line of code:
	 * public static function factory()
		{
			$className = __CLASS__;
			
			if (!self::hasInstance($className)) {
				self::setInstance($className, new $className());
			}
			
			return self::getInstance($className);
		}
	 * 
	 * @param $className name of the model class
	 * @return Plus_Model_Mapper child class object
	 */
	//abstract public static function factory() ;
	
	protected static function hasInstance($className)
	{
		return array_key_exists($className, self::$instancesArray);
	}
	
	protected static function getInstance($className)
	{
		return self::$instancesArray[$className];
	}
	
	protected static function setInstance($className, Plus_Model_Mapper_Abstract $instance)
	{
		self::$instancesArray[$className] = $instance;
	}
}